package Week.w156;

import org.junit.Test;

import java.util.*;

public class Solution01 {

    public boolean uniqueOccurrences(int[] arr) {
        if (arr == null || arr.length == 0) {
            return true;
        }
        Map<Integer, Integer> map = new HashMap<>();

        Set<Integer> set = new HashSet<>();

        for (int key : arr) {
            if (map.containsKey(key)) {
                map.put(key, map.get(key) + 1);
            } else {
                map.put(key, 1);
            }
        }

        for (Map.Entry<Integer, Integer> kvEntry : map.entrySet()) {
            if (set.contains(kvEntry.getValue())) {
                return false;
            }
            set.add(kvEntry.getValue());
        }
        return true;
    }


    public String removeDuplicates1(String s, int k) {
        Stack<Character> stack = new Stack<>();
        int n = s.length();
        for (int i = 0; i < n; i++) {
            if (!stack.isEmpty() && s.charAt(i) == stack.peek()) {
                stack.pop();
            } else {
                stack.push(s.charAt(i));
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        return sb.reverse().toString();
    }

    public String removeDuplicates(String s, int k) {
        int n = s.length(), index = 0;
        char[] arr = new char[n];
        for (int i = 0; i < n; i++) {
            if (index > 0 && index - k + 1 >= 0 && check(arr, index, s.charAt(i), k)) {
                index = index - k + 1;
            } else {
                arr[index++] = s.charAt(i);
            }
        }
        return new String(arr, 0, index);
    }

    private boolean check(char[] arr, int index, char c, int k) {
        for (int i = index - k + 1; i < index; i++) {
            if (arr[i] != c) {
                return false;
            }
        }
        return true;

    }

    public int equalSubstring(String s, String t, int maxCost) {
        int[] costArray = new int[s.length()];
        for (int i = 0; i < s.length(); i++) {
            int num = Math.abs(s.charAt(i) - t.charAt(i));
            costArray[i] = num;
        }
        int[] h = new int[costArray.length+1];

        int sum = 0;
        h[0] = sum;

        for(int i =0; i !=costArray.length; i++){
            sum +=costArray[i];
            h[i+1]=Math.max(sum, h[i]);
        }

        sum = 0;
        int res = 0;
        int pre = 0;
        int len = 0;

        for(int i =0; i!=costArray.length; i++){

            sum +=costArray[i];
            pre = getLessIndex(h, sum-maxCost);
            len = pre == -1 ? 0 : i-pre+1;
            res = Math.max(res, len);

        }
        return res;

    }
    public static int getLessIndex(int[] arr, int num){
        int low = 0;
        int high = arr.length-1;
        int mid = 0;
        int res=-1;
        while(low <= high){
            mid = (low+high)/2;
            if(arr[mid]>=num){
                res=mid;
                high=mid-1;
            }else {
                low = mid +1;
            }
        }

        return res;
    }


    @Test
    public void test() {
        String s0 = "ujteygggjwxnfl";
        String t0 = "nstsenrzttikoy";
        int cost0 = 43;
        System.out.println(equalSubstring(s0, t0, cost0));
 /*       String s = "krrgw";
        String t = "zjxss";
        int cost = 19;
        System.out.println(equalSubstring(s, t, cost));*/
    }
}
